package net.easyjava.framework.web.adapter;

import net.easyjava.framework.BaseConstants;
import net.easyjava.framework.log.LogConstants;
import net.easyjava.framework.web.security.UserSecurityAdminOnCookie;
import net.easyjava.framework.web.security.UserSecurityBean;
import net.easyjava.framework.web.security.UserSecurityOnCookie;
import net.easyjava.tools.http.request.RequestTools;
import net.easyjava.tools.lang.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by wxnacy on 16/7/1.
 */
public class UserSecurityAdminAdapter extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
    public static final String CONTENT_TYPE_TEXT = "text/html;charset=UTF-8";

    @Resource
    private UserSecurityAdminOnCookie userSecurityAdminOnCookie;
    private String noSecurityUrl;
    private int timeout;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        long begin = System.currentTimeMillis();
        String url = RequestTools.getRealUrl(request);
        String relativeUrl = RequestTools.getRelativeUrl(request);

        if(!StringTools.isEmpty(this.noSecurityUrl)){
            String[] surls = this.noSecurityUrl.split(",");
            for(String su:surls){
                if(relativeUrl.equals(su)){
                    long end = System.currentTimeMillis();
                    System.out.printf("noSecurityUrl time:%s", (end - begin));
                    logger.info(LogConstants.API_TIME_SUM, new Object[]{url, (end - begin)});
                    return true;
                }
            }
        }

        if(url.lastIndexOf(".json")>-1){
            long end = System.currentTimeMillis();
            logger.info(LogConstants.API_TIME_SUM, new Object[]{url, (end - begin)});
            return true;
        }

        if(url.lastIndexOf(".html")<0){
            long end = System.currentTimeMillis();
            logger.info(LogConstants.API_TIME_SUM, new Object[]{url, (end - begin)});
            return true;
        }

        UserSecurityBean user = userSecurityAdminOnCookie.getUserSecurityBean(request);
        if(user != null){
            long loginTime = user.getLoginTime();
            if(this.timeout == 0){
                setTimeout(60);
            }
            if(System.currentTimeMillis()-loginTime<1000 * this.timeout * 60){
                long end = System.currentTimeMillis();
                System.out.printf("check time:%s",(end-begin));
                return true;
            }

        }

        logger.info(LogConstants.API_ADAPTER_TIME_SUM, new Object[]{url,"没有登录"});
        long end = System.currentTimeMillis();
        System.out.printf("admin login adapter time:%s", (end - begin));
        response.sendRedirect(BaseConstants.URL_ADMIN_LOGIN_HTML);
        return false;
    }

    public String getNoSecurityUrl() {
        return noSecurityUrl;
    }

    public void setNoSecurityUrl(String noSecurityUrl) {
        this.noSecurityUrl = noSecurityUrl;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}
